import axios from 'axios'
import conf from './http.conf'
import {mutations} from '../store'

const http = axios.create(conf)

// 添加请求拦截器
// 每一次发送请求的时候都会经过(执行)这个函数
http.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  mutations.showLoading()
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})

// 添加响应拦截器
// 每一个响应都会执行这个函数
http.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  mutations.hideLoading()
  return response
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error)
})

http.defaults.withCredentials = true
const locationCityList = (params) => {
  return http.get('v1/cities', { params })
}

const searchCity = (params) => {
  return http.get('v1/pois', { params })
}

const login = (params) => {
  return http.post('v2/login', params)
}

const captachaCode = (params) => {
  return http.post('v1/captchas', params)
}

const reset = (params) => {
  return http.post('v2/changepassword', params)
}
// 获取店铺信息
const shopping = (params) => {
  return http.get('shopping/restaurants', { params })
}
// 商铺详情
const GetDetail = (params) => {
  return http.get('/shopping/restaurant/' + params.ShopId)
}
// 获取店铺搜索信息
const search = (params) => {
  return http.get('v4/restaurants', { params })
}
// 获取食品列表
const ShoppingDetails = (params) => {
  return http.get('shopping/v2/menu', { params })
}
// 获取评价信息
const ShoppingEvaluate = (params) => {
  return http.get('/ugc/v2/restaurants/' + params.restaurant_id + '/ratings', { params })
}
// 获取收货地址
const Address = (params) => {
  return http.get(`/v1/users/${params.user_id}/addresses`, { params })
}
// 添加收货地址
const SaveAddress = (params) => {
  return http.post(`/v1/users/${params.user_id}/addresses`, params)
}
// 删除收货地址
const RemoveAddressById = (params) => {
  return http.delete(`/v1/users/${params.user_id}/addresses/${params.address_id}`)
}
// 获取红包卡券
const ShoppingCoupon = (params) => {
  return http.get(`/promotion/v2/users/${params.user_id}/hongbaos`, { params })
}
// 获取红包卡券
const Orders = (params) => {
  return http.get(`/bos/v2/users/${params.user_id}/orders`, { params })
}

/* const shopCart = params => http.get()

} */

export default {
  locationCityList,
  searchCity,
  login,
  captachaCode,
  reset,
  shopping,
  GetDetail,
  search,
  ShoppingDetails,
  ShoppingEvaluate,
  Address,
  SaveAddress,
  RemoveAddressById,
  ShoppingCoupon,
  Orders
}
